package org.dfl.plugin.editor.commands;

import org.dfl.plugin.autoalign.DflDiagramLayout;
import org.dfl.plugin.editor.model.DflDiagram;
import org.eclipse.gef.commands.Command;

/**
 * Auto-aligns the diagram horizontally or vertically using graphviz package.
 * @author Piotr Wlodarczyk
 *
 */
public class AutoAlignCommand extends Command {
	DflDiagramLayout layout;
	DflDiagram diagram;
	
	/**
	 * create a new align command
	 * @param diagram - diagram to align
	 * @param vertically - true if vertically, false if horizontally
	 */
	public AutoAlignCommand(DflDiagram diagram, boolean vertically) {
		super();
		this.diagram = diagram;
		this.layout = new DflDiagramLayout(diagram, vertically);
	}

	@Override
	public boolean canExecute() {
		return (diagram!=null && diagram.isEditable());
	}

	@Override
	public void execute() {
		layout.computeNewLocations();
		redo();
	}

	@Override
	public boolean canUndo() {
		return (diagram!=null && diagram.isEditable());
	}
	
	@Override
	public void undo() {
		layout.setOldLocations();
		diagram.getInput().focus();
	}

	@Override
	public void redo() {
		layout.setNewLocations();
		diagram.getInput().focus();
	}

}
